package com.company.project.web;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.ThesisAssessment;
import com.company.project.service.ThesisAssessmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
* Created by CodeGenerator on 2018/10/08.
*/
@RestController
@RequestMapping("/thesis/assessment")
public class ThesisAssessmentController {
    @Resource
    private ThesisAssessmentService thesisAssessmentService;

    @PostMapping("/add")
    public Result add(ThesisAssessment thesisAssessment) {
        thesisAssessmentService.save(thesisAssessment);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 前台查询投稿评估
     * @param id
     * @return
     */
    @GetMapping("/selectAssess/{id}")
    @ResponseBody
    public Result selectAssess(@PathVariable("id") Integer id){
        List<Map<Object, Object>> maps = thesisAssessmentService.selectAssess(id);
        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Map<Object,Object> map : maps){
            map.put("create_time",format.format(map.get("create_time")));
        }
        return ResultGenerator.genSuccessResult(maps);
    }
    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        thesisAssessmentService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(ThesisAssessment thesisAssessment) {
        thesisAssessmentService.update(thesisAssessment);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        ThesisAssessment thesisAssessment = thesisAssessmentService.findById(id);
        return ResultGenerator.genSuccessResult(thesisAssessment);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ThesisAssessment> list = thesisAssessmentService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("selectById/{id}")
    public ThesisAssessment thesisAssessment(@PathVariable("id") Integer id){
        return thesisAssessmentService.getThesisAssessmentById(id);
    }
}
